package cm309.BookingSystem.tm.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cm309.BookingSystem.common.MyConnection;
import cm309.BookingSystem.web.Action;


public class DeleteTMAction implements Action {

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		MyConnection con=new MyConnection();
		try {
			
			
			//t.setTrainerID(Long.toString(oldtrainerIDnum));
			String trainerID=request.getParameter("trainerID");
	
			//delete
			PreparedStatement ps=con.getPS(
					"Delete BK_TM_TRAINER WHERE TRAINERID=?");
	
			ps.setString(1,trainerID);
			int count=ps.executeUpdate();
			if (count!=1) {
				throw new RuntimeException("Trainer no found");
			}
			
			con.commit();
			con.PSclose();
			
			 //redirect to the show product!
			PrintWriter w = response.getWriter();
		
			w.println("{\"success\":true}");
			  			
		} catch (SQLException e) {

			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			con.close();
		}

	}

}
